<template>
  <div class="app-container">

    <div class="filter-container">
      <el-button-group>
        <el-button class="filter-item"
                   size="mini"
                   type="primary"
                   icon="el-icon-edit"
                   @click="handleCreate">添加
        </el-button>
        <el-button class="filter-item"
                   size="mini"
                   type="danger"
                   icon="el-icon-delete"
                   @click="handleDelete">删除
        </el-button>
        <el-button class="filter-item"
                   size="mini"
                   type="warning"
                   icon="el-icon-edit"
                   @click="handleDeduction">扣减设计数
        </el-button>
      </el-button-group>
    </div>
    <el-table :key="tableKey"
              :data="list"
              border
              fit
              highlight-current-row
              style="width: 100%;"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection"
                       width="55"
                       align="center"></el-table-column>
      <el-table-column label="等级名称"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="等级"
                       align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type===1">{{ scope.row.level }}</span>
        </template>
      </el-table-column>
      <el-table-column label="显示隐藏"
                       align="center">
        <template slot-scope="scope">
          <el-switch :active-value="1"
                     :inactive-value="0"
                     v-model="scope.row.isShow"
                     @change="isShowChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否为初始等级"
                       align="center">
        <template slot-scope="scope">
          <el-switch :active-value="1"
                     :inactive-value="0"
                     v-if="scope.row.type===1"
                     v-model="scope.row.isInitLevel"
                     @change="isInitLevelChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="上传素材数量"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.uploadTrendsNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传商品数量"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.uploadGoodsNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定达人数量"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.bindExpertNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="存储大小"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.storageQuantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上推荐数量"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.recommendNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推荐币"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.virtualCurrency }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ui设计数"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.uiLayoutNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品主图设计"
                       align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.goodsMainImgDesignNum }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')"
                       class-name="small-padding"
                       fixed="right"
                       min-width="150">
        <template slot-scope="scope">
          <el-button type="primary"
                     icon="el-icon-edit"
                     size="mini"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button type="primary"
                     icon="el-icon-edit"
                     size="mini"
                     @click="showLevelPrice(scope.row)">查看价格
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="listQuery.page"
                :limit.sync="listQuery.pageSize"
                @pagination="getList"/>

    <!--添加编辑框-->
    <el-dialog :title="dialogType==='edit'?'编辑':'新增'"
               :visible.sync="dialogFormVisible">
      <el-form ref="dataForm"
               :model="config"
               label-width="100px"
               label-position="right"
               style="width: 100%; margin-left: 50px;">
        <el-form-item label="显示隐藏"
                      prop="isShow"
        >
          <el-switch v-model="config.isShow"
                     :active-value="1"
                     :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="类型"
                      prop="type"
        >
          <el-select v-model="config.type" placeholder="请选择">
            <el-option
              v-for="item in typeOptions"
              :key="item.key"
              :label="item.text"
              :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="初始等级"
                      prop="isInitLevel" v-if="config.type===1"
        >
          <el-switch v-model="config.isInitLevel"
                     :active-value="1"
                     :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="排序"
                      prop="sort"
                      v-if="config.type===1"
                      :rules="[{ required: true, message: '请填写等级', trigger: 'blur'}]">
          <el-input v-model="config.sort"
                    type="number"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="等级"
                      v-if="config.type===1"
                      prop="level"
                      :rules="[{ required: true, message: '请填写等级', trigger: 'blur'}]">
          <el-input v-model="config.level"
                    type="number"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="等级名称"
                      prop="name"
                      :rules="[{ required: true, message: '请填写服务名称', trigger: 'blur' }]">
          <el-input v-model="config.name"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="等级描述"
                      prop="levelDescribe"
                      :rules="[{ required: true, message: '请填写等级描述', trigger: 'blur' }]">
          <el-input type="textarea"
                    v-model="config.levelDescribe"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="上传商品数量"
                      prop="uploadGoodsNum"
                      v-if="config.type===1"
                      :rules="[{ required: true, message: '请填写上传商品数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.uploadGoodsNum"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="上传素材数量"
                      prop="uploadTrendsNum"
                      v-if="config.type===1||config.type===3"
                      :rules="[{ required: true, message: '请填写上传素材数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.uploadTrendsNum"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="存储大小"
                      prop="storageQuantity"
                      v-if="config.type===1||config.type===2"
                      :rules="[{ required: true, message: '请填写存储大小', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.storageQuantity"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="上推荐数量"
                      prop="recommendNum"
                      v-if="config.type===1"
                      :rules="[{ required: true, message: '请填写上推荐数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.recommendNum"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="绑定达人数量"
                      prop="bindExpertNum"
                      v-if="config.type===1"
                      :rules="[{ required: true, message: '请填写绑定达人数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.bindExpertNum"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="推荐币数量"
                      prop="virtualCurrency"
                      v-if="config.type===4"
                      :rules="[{ required: true, message: '请填写推荐币数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.virtualCurrency"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="ui设计数"
                      prop="uiLayoutNum"
                      v-if="config.type===1"
                      :rules="[{ required: true, message: '请填写ui设计数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.uiLayoutNum"
                    style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="商品主图设计"
                      prop="goodsMainImgDesignNum"
                      v-if="config.type===1"
                      :rules="[{ required: true, message: '请填写商品主图设计数量', trigger: 'blur' }]">
          <el-input type="number"
                    v-model="config.goodsMainImgDesignNum"
                    style="width: 60%"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger"
                   @click="dialogFormVisible=false">取消
        </el-button>
        <el-button type="primary"
                   @click="confirm">确定
        </el-button>
      </div>
    </el-dialog>

    <LevelPrice :visible.sync="showLevelPriceView"
                :levelItem="currentLevel"
                v-if="showLevelPriceView"></LevelPrice>
    <DeductionDesignNum :visible.sync="showDeductionDesignNum"  ref="child"
               ></DeductionDesignNum>
  </div>
</template>

<script>
import {getListShopLevel, editSubmitLevel, deleteShopLevel} from '@/api/shopLevel'
import waves from '@/directive/waves' // Waves directive
import {parseTime, renderTime, moneyFormat} from '@/utils'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import LevelPrice from '../level/levelPrice'
import DeductionDesignNum from '../level/deductionDesignNum'
import DeductionDesignNumLog from "./deductionDesignNumLog.vue";
const typeOptions = [
  { key: 1, text: '会员等级' },
  { key: 2, text: '增值服务-内存扩容' },
  { key: 3, text: '增值服务-条数扩容' },
  { key: 4, text: '推荐币' },
]
export default {
  name: 'shopService',
  components: {DeductionDesignNumLog, Pagination, LevelPrice,DeductionDesignNum},
  directives: {waves},
  data() {
    return {
      showDeductionDesignNum:false,
      typeOptions,
      currentLevel: {},
      showLevelPriceView: false,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
      },
      config: {},
      dialogFormVisible: false,
      dialogType: '',
      multipleSelection: [],
      locationActiveName: 'first',
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleDeduction(){
      this.showDeductionDesignNum=true
      this.$nextTick(() => {
        this.$refs.child.intDate();
      })
    },
    showLevelPrice(item) {
      this.currentLevel = item
      this.showLevelPriceView = true
    },
    getList() {
      this.listLoading = true
      let queryParams = this.listQuery
      getListShopLevel(queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },

    handleCreate() {
      this.dialogType = 'add'
      this.dialogFormVisible = true
      this.config = {status: 1}
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.dialogType = 'edit'
      this.dialogFormVisible = true
      this.config = Object.assign({}, row) // copy obj
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let formData = Object.assign({}, this.config)
          formData.addTime = null
          formData.updTime = null
          editSubmitLevel(formData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    handleDelete() {
      const delVals = this.multipleSelection;
      if (!delVals || delVals.length <= 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error',
          duration: 2000
        })
        return;
      }


      let flag = false
      this.multipleSelection.forEach(item => {
        if (item.isInitLevel === 1) {
          flag = true
        }
      })

      if (flag) {
        this.$message({
          message: '至少要有一个初始等级',
          type: 'error',
          duration: 2000
        })
      } else {
        this.$confirm('您确定要删除这些记录?', '提醒', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let values = [];
          delVals.forEach(val => {
            values.push(val.id);
          })
          deleteShopLevel(values.join(',')).then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
        })
          .catch(err => {
            console.error(err)
          })
      }


    },

    isInitLevelChange(row) {
      let formData = {
        isInitLevel: row.isInitLevel,
        id: row.id
      }
      editSubmitLevel(formData).then(() => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      }).catch(err => {
        this.getList()
      })
    },
    isShowChange(row) {
      let formData = {
        isShow: row.isShow,
        id: row.id
      }
      editSubmitLevel(formData).then(() => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },


  }
}
</script>
